<html>
  
  <head>
    <meta charset="utf-8">
    <title>Bootstrap, from Twitter</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="">
    <link href="http://netdna.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css"
    rel="stylesheet">
    <script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
    <script type="text/javascript" src="http://netdna.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
  </head>
  
  <body>
    <div class="container">
      <div class="row">
      <div class="col-sm-6">
          <h1>Resultado do automato</h1>
      

<?php
$d = "upload/";
$f = $d. "tabela.txt";
$t = $d."entrada.txt";
if(move_uploaded_file($_FILES['tabela']['tmp_name'], $f)){
    $a = file_get_contents($f);
    $str = str_replace("\t\t", "\\|/", $a);
    $str = str_replace("\t", "\\|/", $str);
    $tabela = explode("\n", $str);
    $l = $tabela[0];
    array_shift($tabela);
    if(empty($tabela[count($tabela)-1])){
        array_pop($tabela);
    }
    if(ctype_graph($l)){
        //echo "Nada de mais<br>";
    }else{
        $l[strlen($l)-1] = "\n";
        //echo "Tem algo errado";
    }
    $l = str_replace("\n","",$l);
    $l = str_replace("\0","",$l);
    //$l = str_replace("\","",$l);
    
    $l = explode("\\|/", $l);
    //echo var_dump($l);
    $nstados = count($tabela);
    $sinicial = 0;
    $sfinal = array();
    for ($i = 0; $i < $nstados; $i++){
        if(gettype(strpos($tabela[$i], ">")) == "integer"){
            $sinicial = $i;
        }
        if(gettype(strpos($tabela[$i], "*")) == "integer"){
            array_push($sfinal, $i);
        }
        $tabela[$i] = str_replace("q", "", $tabela[$i]);
        $tabela[$i] = str_replace("\n", "", $tabela[$i]);
        $tabela[$i] = str_replace(">", "", $tabela[$i]);
        $tabela[$i] = str_replace("*", "", $tabela[$i]);
        $tabela[$i] = str_replace("-", "-1", $tabela[$i]);
        $tabela[$i] = explode("\\|/", $tabela[$i]);
    }
    for ($i=0;$i<$nstados;$i++){
        for($j=0;$j<count($l);$j++){
            $tabela[$i][$j] = intval($tabela[$i][$j]);
        }
    }
    if(move_uploaded_file($_FILES['entrada']['tmp_name'], $t)){
        $e = file_get_contents($t);
        $entrada = explode("\n", $e);
        if(empty($entrada[count($entrada)-1])){
            array_pop($entrada);
        }
        
        $saida = fopen("upload/resultado.txt", 'w+');
        for($i=0;$i<count($entrada);$i++){
            $atual = $sinicial;
            //echo "INICIAIL ".$atual."\n";
            if(ctype_graph($entrada{$i})){
        //echo "Nada de mais<br>";
    }else{
        $entrada[$i] = substr($entrada[$i],0,strlen($entrada[$i])-1);
        //echo "Ach ".$entrada[$i];
        //echo "Tem algo errado";
    }
            $t = strlen($entrada[$i]);
            for($j=0;$j<$t;$j++){
                if(!array_search($entrada[$i][$j], $l)){
                    $atual = -1;
                    echo "PAROU";
                    break;
                }
                $n = array_search($entrada[$i][$j], $l);
                //echo "Posição na tabela ".$n."<br>";
                $atual = $tabela[$atual][$n];
                if($atual == -1){
                    echo "PAROU NO SEGUNDO";
                    break;
                }
            }
            //echo "<br>";
            if(in_array($atual, $sfinal)){
                echo "aceita<br>";
                fwrite($saida, "aceita\n");
            }else{
                echo "não aceita<br>";
                fwrite($saida, "nao aceita\n");
            }
            
        }
        echo "<a href=\"download.php\" class=\"btn btn-primary\">Download resultado em txt</a>";
    }else{
        echo "É precio informar o arquivo contendo as strings de entrada";

    }
}else{
    echo "É preciso informar o arquivo com a tabela de transições";
}
?>
</div>
</div>
      </div>
      </body>
      </html>
